//Just showing a difference

/* 5/26/2013 - A lot of changes.. small summary:
 * cleaned up xml warnings
 * SLIGHTLY adjusted layout, for better or worse
 * added toggle button to show/hide binary answer
 */

package com.example.binary_test;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity 
{

	Random rand_gen;
	
	boolean binary_number_inputed = false;
	
	public static final int MAX_BINARY_DIGITS = 8;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		init_vars();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void add_0(View view) 
	{
		update_binary_input_number("0");
	}

	public void add_1(View view) 
	{
		update_binary_input_number("1");
	}

	public void backspace(View view) 
	{
		TextView input_binary_number = (TextView) findViewById(R.id.input_binary_number);
		String curr_string = input_binary_number.getText().toString();

		if (curr_string.length() > 0) 
		{
			curr_string = curr_string.substring(0, curr_string.length() - 1);
		}

		input_binary_number.setText(curr_string);
	}

	private void check_correct(String input_binary_number) 
	{
		TextView rand_num_binary = (TextView) findViewById(R.id.rand_num_binary);
		TextView num_wrong = (TextView) findViewById(R.id.num_wrong);
		TextView num_correct = (TextView) findViewById(R.id.num_correct);
		String num_curr;
		int num_convert;
		Integer input_num = Integer.parseInt(input_binary_number);
		Integer random_num = Integer.parseInt(rand_num_binary.getText().toString());
		
		
		if (input_num == random_num) 
		{
			num_curr = num_correct.getText().toString();

			num_convert = Integer.parseInt(num_curr);
			num_convert++;

			num_correct.setText(Integer.toString(num_convert));
		} 
		
		else 
		{
			num_curr = num_wrong.getText().toString();

			num_convert = Integer.parseInt(num_curr);
			num_convert++;

			num_wrong.setText(Integer.toString(num_convert));
		}
	}

	private String convert_to_binary(int num_int, int num_bits) 
	{
		String converted_num = "";

		int num_adjusted = num_int;

		for (int pow_curr = num_bits - 1; pow_curr >= 0; pow_curr--) 
		{
			if (Math.pow(2, pow_curr) <= num_adjusted) 
			{
				num_adjusted -= Math.pow(2, pow_curr);

				converted_num += "1";
			} 
			
			else 
			{
				converted_num += "0";
			}
		}
		return converted_num;
	}

	private void init_vars() 
	{
		
		rand_gen = new Random();
		toggle_button_state(false);
	}

	private void toggle_button_state(boolean enable)
	{
		Button zero = (Button) findViewById(R.id.zero_btn);
		Button one = (Button) findViewById(R.id.one_btn);
		Button delete = (Button) findViewById(R.id.backspace_btn);
		Button enter = (Button) findViewById(R.id.enter_button);
		
	
		zero.setClickable(enable);
		one.setClickable(enable);
		delete.setClickable(enable);
		enter.setClickable(enable);	
	
	}
	private void update_binary_input_number(String number_to_append) 
	{
		TextView input_binary_number = (TextView) findViewById(R.id.input_binary_number);
		String curr_string = "";

		if (!binary_number_inputed) 
		{
			binary_number_inputed = true;
		} else {
			curr_string = input_binary_number.getText().toString();
		}

		if (curr_string.length() < MAX_BINARY_DIGITS) 
		{
			curr_string += number_to_append;
			input_binary_number.setText(curr_string);
		}

//		if (curr_string.length() == MAX_BINARY_DIGITS) 
//		{
//			
//		}
	}

	public void update_random_number(View view) 
	{
		int rand_num = rand_gen.nextInt(256);

		TextView rand_num_int = (TextView) findViewById(R.id.rand_num_int);
		TextView rand_num_binary = (TextView) findViewById(R.id.rand_num_binary);
		TextView input_binary_number = (TextView) findViewById(R.id.input_binary_number);

		rand_num_int.setText(Integer.toString(rand_num));
		rand_num_binary.setText(convert_to_binary(rand_num, MAX_BINARY_DIGITS));

		input_binary_number.setText("Input Binary Number");
		binary_number_inputed = false;
		toggle_button_state(true);
	}
	
	//toggle_answer function, making the game slightly more fun to test by using ToggleButton to show/hide answer (rand_num_binary)//////
	//xml for TextView rand_num_bianry had default visibility set to "invisible"
	/*   http://developer.android.com/guide/topics/ui/controls/togglebutton.html   */
	public void toggle_answer(View view) 
	{
		TextView rand_num_binary = (TextView) findViewById(R.id.rand_num_binary);
		
	    boolean on = ((ToggleButton) view).isChecked();
	    
	    if (on) 
	    {
	        rand_num_binary.setVisibility(View.VISIBLE);
	    } 
	    
	    else 
	    {
	    	rand_num_binary.setVisibility(View.INVISIBLE);
	    }
	}
	
	public void enter_button_press(View view)
	{
		TextView input_binary_number = (TextView) findViewById(R.id.input_binary_number);
		
		if(input_binary_number.getText().length() == MAX_BINARY_DIGITS)
		{
			check_correct(input_binary_number.getText().toString());
			input_binary_number.setText("Input Binary Number");
			binary_number_inputed = false;
			update_random_number(view);
		}
	}
}
